<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="打印机名称" prop="printerName">
            <el-input
                v-model="queryParams.printerName"
                placeholder="请输入打印机名称"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="打印机编号" prop="printerCode">
            <el-input
                v-model="queryParams.printerCode"
                placeholder="请输入打印机编号"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="识别码" prop="printerSecret">
            <el-input
                v-model="queryParams.printerSecret"
                placeholder="请输入打印机识别码"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="自动打印" prop="printerAutoPrint">
            <el-select
                v-model="queryParams.printerAutoPrint"
                placeholder="请选择是否自动打印"
                clearable
                style="width: 100%"
            >
              <el-option label="是" value="是" />
              <el-option label="否" value="否" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="打印机品牌" prop="printerBrand">
            <el-input
                v-model="queryParams.printerBrand"
                placeholder="请输入打印机品牌"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <div style="display: flex; justify-content: space-evenly; width: 100%;">
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:printer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:printer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:printer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:printer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="printerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键ID" align="center" prop="printerId" />-->
      <!--<el-table-column label="商家ID" align="center" prop="printerMerchantId" />-->
      <el-table-column label="打印机名称" align="center" prop="printerName" />
      <el-table-column label="打印机编号" align="center" prop="printerCode" />
      <el-table-column label="打印机密钥/识别码" align="center" prop="printerSecret" />
      <el-table-column label="自动打印" align="center" prop="printerAutoPrint" />
      <el-table-column label="打印机品牌" align="center" prop="printerBrand" />
      <el-table-column label="创建时间" align="center" prop="printerCreateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.printerCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="printerUpdateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.printerUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="删除标志" align="center" prop="printerDelFlag" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:printer:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:printer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改打印机列表对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="printerRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="商家ID" prop="printerMerchantId">
          <el-input v-model="form.printerMerchantId" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="打印机名称" prop="printerName">
          <el-input v-model="form.printerName" placeholder="请输入打印机名称" />
        </el-form-item>
        <el-form-item label="打印机编号" prop="printerCode">
          <el-input v-model="form.printerCode" placeholder="请输入打印机编号" />
        </el-form-item>
        <el-form-item label="打印机密钥/识别码" prop="printerSecret">
          <el-input v-model="form.printerSecret" placeholder="请输入打印机密钥/识别码" />
        </el-form-item>
        <el-form-item label="是否自动打印" prop="printerAutoPrint">
          <el-input v-model="form.printerAutoPrint" placeholder="请输入接单后是否自动打印" />
        </el-form-item>
        <el-form-item label="打印机品牌" prop="printerBrand">
          <el-input v-model="form.printerBrand" placeholder="请输入打印机品牌" />
        </el-form-item>
        <el-form-item label="创建时间" prop="printerCreateTime">
          <el-date-picker clearable
                          v-model="form.printerCreateTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="printerUpdateTime">
          <el-date-picker clearable
                          v-model="form.printerUpdateTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <!--<el-form-item label="删除标志" prop="printerDelFlag">-->
        <!--  <el-input v-model="form.printerDelFlag" placeholder="请输入删除标志" />-->
        <!--</el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Printer">
import { listPrinter, getPrinter, delPrinter, addPrinter, updatePrinter } from "@/api/order/printer.js"

const { proxy } = getCurrentInstance()

const printerList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    printerMerchantId: null,
    printerName: null,
    printerCode: null,
    printerSecret: null,
    printerAutoPrint: null,
    printerBrand: null,
    printerCreateTime: null,
    printerUpdateTime: null,
    printerDelFlag: null
  },
  rules: {
    printerMerchantId: [
      { required: true, message: "商家ID不能为空", trigger: "blur" }
    ],
    printerName: [
      { required: true, message: "打印机名称不能为空", trigger: "blur" }
    ],
    printerCode: [
      { required: true, message: "打印机编号不能为空", trigger: "blur" }
    ],
    printerSecret: [
      { required: true, message: "打印机密钥/识别码不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询打印机列表列表 */
function getList() {
  loading.value = true
  listPrinter(queryParams.value).then(response => {
    printerList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    printerId: null,
    printerMerchantId: null,
    printerName: null,
    printerCode: null,
    printerSecret: null,
    printerAutoPrint: null,
    printerBrand: null,
    printerCreateTime: null,
    printerUpdateTime: null,
    printerDelFlag: null
  }
  proxy.resetForm("printerRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.printerId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加打印机列表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _printerId = row.printerId || ids.value
  getPrinter(_printerId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改打印机列表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["printerRef"].validate(valid => {
    if (valid) {
      if (form.value.printerId != null) {
        updatePrinter(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPrinter(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _printerIds = row.printerId || ids.value
  proxy.$modal.confirm('是否确认删除打印机列表编号为"' + _printerIds + '"的数据项？').then(function() {
    return delPrinter(_printerIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/printer/export', {
    ...queryParams.value
  }, `printer_${new Date().getTime()}.xlsx`)
}

getList()
</script>
